package com.cn.esermis.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class TableUtils {

//    public static String tableList;
    @Resource
    private JdbcTemplate jdbcTemplate;

    public static List<String> existingTables = new ArrayList<>();

    public List<String> getExistingTables() {
        if (existingTables.isEmpty()) {
            refreshExistingTables();
        }
        return existingTables;
    }

    public void refreshExistingTables() {
        String sql = "SHOW TABLES";
        existingTables = jdbcTemplate.queryForList(sql, String.class);
        System.out.println(existingTables);
    }

    public boolean isTableExists(String tableName) {
        return getExistingTables().contains(tableName);
    }
}